<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script src="js/angular.min.js"></script>
    
    <style>
   table, th , td {
      border: 1px solid grey;
      border-collapse: collapse;
      padding: 5px;
   }
   table tr:nth-child(odd) {
      background-color: #f2f2f2;
   }
   table tr:nth-child(even) {
      background-color: #ffffff;
   }
   .redClass {
     background-color:red;
   }
</style>
</head>
<body ng-app="myApp">
	<label>wget http://ip:1072/luluinner.sh </label><br/>

<div ng-controller="uploadCtrl">
	
	<div>
	   <label>
	       Cover Port:
	    <input  type="text" name="coverPort" ng-model="coverPort" required/>
	    </label><br/>
	
		<div ng-repeat="pigeon in pigeons">
			<div>
				<label>
				<input name="shadow" type="radio" ng-model="$parent.shadow"  ng-value="pigeon"/>
				{{pigeon.code}}{{pigeon.address}}
				</label>
			</div>
		</div>
		
	    <label>
	       Mole Ip:
	      <input  type="text" name="moleIp" ng-model="moleIp" required/>
	    </label><br/>
	   <label>
	       Mole Port:
	      <input  type="text" name="molePort" ng-model="molePort" required/>
	    </label><br/>
	
	   <label>{{errorMsg}}</label><br/>
	   <button ng-click="lurk()">Lurk</button>
	</div>
    Hello, World~
    
	<div >
	  <div>{{mole}}</div>
	</div>
	
<table>
   <tr>
      <th>Cover Port</th>
      <th>Pigeon Code</th>
      <th>Pigeon Address</th>
      <th>Mole Ip</th>
      <th>Mole Port</th>
      <th>Operation</th>
   </tr>
   
   <tr ng-repeat="mole in moles">
      <td>{{ mole.coverPort }}</td>
      <td ng-class="{'redClass': !mole.status}">{{ mole.pigeonCode }}</td>
      <td ng-class="{'redClass': !mole.status}">{{ mole.pigeonAddress }}</td>
      <td>{{ mole.moleIp }}</td>
      <td>{{ mole.molePort }}</td>
      <td><button ng-click="delmole( mole.coverPort )">D</button></td>
   </tr>
</table>
	
	
</div>

    
    
    <script>
var app = angular.module('myApp', []);
app.controller('uploadCtrl', function($scope, $http) {
	
	  $scope.coverPort = '37376';
	  $scope.moleIp = '127.0.0.1';
	  $scope.molePort = '22';


	  $http.get("/moles")
	  .then(function (response) {$scope.moles = response.data;});
	
  $http.get("/pigeons")
  .then(function (response) {
	  $scope.pigeons = response.data;
	  if($scope.pigeons.length > 0)
		  {
		  $scope.shadow = $scope.pigeons[0];
		  }
	  });
  
  
  $scope.lurk = function() {
	  $scope.errorMsg = '';
	  
	  var coverPort = parseInt($scope.coverPort);
	  if(!coverPort) {
		  $scope.errorMsg = "error coverPort";
		  return;
	  }
	  
	  if(!isValidIP($scope.moleIp))
	  {
	  $scope.errorMsg = "error moleIp";
	  return;
	  }
	  
	  var molePort = parseInt($scope.molePort);
	  if(!molePort) {
		  $scope.errorMsg = "error molePort";
		  return;
	  }
	  
	  var params = { };
	  params.coverPort = coverPort;
	  params.pigeonCode = $scope.shadow.code;
	  params.pigeonAddress = $scope.shadow.address;
	  params.moleIp = $scope.moleIp;
	  params.molePort = molePort;
	  console.log(JSON.stringify(params));

	  $http.post("/addmole", params)
	  .success(function (response) {
		  console.log(response);
			
		  $scope.coverPort = '';
		  $scope.moleIp = '';
		  $scope.molePort = '';
		  
		  $http.get("/moles")
		  .then(function (response) {$scope.moles = response.data;});
		  
		  });
  }
  
  $scope.delmole = function(coverPort) {

	  $http.delete("/delmole?coverPort="+coverPort)
	  .success(function (response) {
		  console.log(response);
		  
		  $http.get("/moles")
		  .then(function (response) {$scope.moles = response.data;});
		  });
  }

  function isValidIP(ip)
  {   
      var reg =  /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      
      return reg.test(ip);   
  }
   
	});
</script>
</body>
</html>
